home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / stk-3.002 / stk-3 / STk-3.1 / Src / dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-02  |  4.6 KB  |  176 lines

  1. /*
  2.  * d u m p . c                -- Image creation
  3.  *
  4.  * Copyright ⌐ 1993-1996 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
  5.  * 
  6.  *
  7.  * Permission to use, copy, and/or distribute this software and its
  8.  * documentation for any purpose and without fee is hereby granted, provided
  9.  * that both the above copyright notice and this permission notice appear in
  10.  * all copies and derived works.  Fees for distribution or use of this
  11.  * software or derived works may only be charged with express written
  12.  * permission of the copyright holder.  
  13.  * This software is provided ``as is'' without express or implied warranty.
  14.  *
  15.  * This software is a derivative work of other copyrighted softwares; the
  16.  * copyright notices of these softwares are placed in the file COPYRIGHTS
  17.  *
  18.  *           Author: Erick Gallesio [eg@unice.fr]
  19.  *    Creation date: ??-Jul-1993 ??:??
  20.  * Last file update:  2-Jun-1996 21:39
  21.  */
  22.  
  23. #include "stk.h"
  24.  
  25. int STk_dumped_core = 0;
  26.  
  27. #if defined (SUNOS4) || defined(FREEBSD) || defined(LINUX)
  28. static caddr_t current_break     = (caddr_t) -1;
  29. static long data_size         = 0;
  30. static long data_start        = 0;
  31. static int restoring_image    = 0;
  32.  
  33. #include <a.out.h>
  34. #include <fcntl.h>
  35. #include <stdio.h>
  36. #include <setjmp.h>
  37. #include <sys/types.h>
  38. #include <sys/time.h>
  39.  
  40. #define TEXT_START(x)    (N_TXTADDR(x)+(sizeof(x)-N_TXTOFF(x)))
  41. #define TEXT_SIZE(x)    ((x).a_text - (sizeof(x)-N_TXTOFF(x)))
  42. #define DATA_START(x)    (N_DATADDR(x))
  43.  
  44.  
  45. static int dump_data_file(char *argv0, char *name)
  46. {
  47.   int fd1, fd2;
  48.   struct exec header;
  49.  
  50.   /* find the header of current running program */
  51.   if ((fd1 = open(argv0, O_RDONLY)) < 0) 
  52.     Err("dump: cannot open myself", STk_makestring(argv0));
  53.  
  54.   read(fd1, &header, sizeof header);
  55.   
  56.   /* Now that this header is read, create the new file */
  57.   if ((fd2=open(name, O_WRONLY|O_CREAT|O_TRUNC, 0755)) < 0) {
  58.     close(fd1);
  59.     Err("dump: cannot open file", STk_makestring(name));
  60.   }
  61.  
  62.   /* write in fd2 the current sbrk followed by data segment size */
  63.   current_break = (caddr_t) sbrk(0);
  64.   data_size     = (char *)current_break - (char *) DATA_START(header);
  65.   data_start    = DATA_START(header);
  66.  
  67.   write(fd2, ¤t_break, sizeof(caddr_t));
  68.   write(fd2, &data_size,     sizeof(long));
  69.   write(fd2, &data_start,    sizeof(long));
  70.  
  71.   /* Copy data segment */
  72.   write(fd2, (void *)DATA_START(header), data_size);
  73.   
  74.   close(fd1); close(fd2);
  75.   return 1;
  76. }
  77.  
  78.  
  79. static int Restore_data_file(char *name)
  80. {
  81.   int fd;
  82.  
  83.   /* find the header of current running program */
  84.   if ((fd = open(name, O_RDONLY)) < 0) 
  85.     Err("Cannot open image file", STk_makestring(name));
  86.  
  87.   /* read the break we have to set and data segment size */
  88.   read(fd, ¤t_break, sizeof(caddr_t));
  89.   read(fd, &data_size,     sizeof(long));
  90.   read(fd, &data_start,    sizeof(long));
  91.  
  92.   /* read data segment */
  93.   brk(current_break);
  94.   read(fd, (void *)data_start, data_size);
  95.  
  96.   close(fd); 
  97.   return 1;
  98. }
  99.  
  100.  
  101. static void internal_dump(char *s)
  102. {
  103.   /* Store current continuation in a global Scheme variable */
  104.   STk_eval_C_string("(define *global-continuation* (call/cc (lambda(e) e)))", NIL);
  105.  
  106.   if (restoring_image) {
  107.     /* 
  108.      * Since the primitive dump is in the call stack when we saved 
  109.      * continuation, we go back here on image restoration. If restoring_image is
  110.      * is equal to 1, we are restoring an image, so we can return.
  111.      */
  112.     return;
  113.   }
  114.  
  115.   STk_dumped_core = 1;
  116.   dump_data_file(STk_Argv0, CHARS(STk_internal_expand_file_name(s)));
  117.   STk_dumped_core = 0;
  118. }
  119.  
  120.  
  121. void STk_restore_image(char *s)
  122. {
  123.   SCM gcont;
  124.  
  125.   Restore_data_file(s);
  126.   STk_dumped_core =  restoring_image = 1;
  127.  
  128.   gcont = VCELL(Intern("*global-continuation*"));
  129.   /* After reading the file we must have a continuation in *global-continuation* */
  130.   if (NCONTINUATIONP(gcont)) {
  131.     Err("restore: file loaded is corrupted. DANGER.", NIL);
  132.   }
  133.   Apply(gcont, LIST1(Ntruth));
  134. }
  135.  
  136. /******************************************************************************
  137.  *
  138.  * d u m p   p r i m i t i v e 
  139.  *
  140.  ******************************************************************************/
  141.  
  142. PRIMITIVE STk_dump(SCM s)
  143. {
  144.   if (NSTRINGP(s)) Err("dump: bad file name", s);
  145. #ifdef USE_TK
  146.   if (Tk_initialized) Err("dump: cannot dump an image if you have "
  147.               "not used the `-no-tk' option.\nSorry.", NIL);
  148. #endif
  149.   internal_dump(CHARS(s));
  150.   return UNDEFINED;
  151. }
  152.  
  153. #else
  154.  
  155. /********************************************/
  156. /****                     ****/
  157. /**** Architectures without Dump support ****/
  158. /****                     ****/
  159. /********************************************/
  160.  
  161. static void dump_error()
  162. {
  163.   Err("dump/restore not available on this architecture", NIL);
  164. }
  165.  
  166. void STk_restore_image(char *s)
  167. {
  168.   dump_error();
  169. }
  170.  
  171. PRIMITIVE STk_dump(SCM s)
  172. {
  173.   dump_error();
  174. }
  175. #endif
  176.